/**
 * 
 */
package com.leyu.mall.site.shiro.ext;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;  

import com.leyu.mall.common.utils.Encodes;
import com.leyu.mall.domain.user.UserBase;
import com.leyu.mall.service.user.UserService;

/**
 * @author change
 * 
 */
@Service
public class SiteAuthorizingRealm extends AuthorizingRealm {
	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024; 
	@Resource
	private UserService userService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.shiro.realm.AuthorizingRealm#doGetAuthorizationInfo(org.apache
	 * .shiro.subject.PrincipalCollection)
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(
			PrincipalCollection principal) {
		UerModel userModel = (UerModel) principal.getPrimaryPrincipal();
		UserBase user = userService.findByLoginName(userModel
				.getUsername());
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		/*info.addRoles(user.getRoleList());*/
		return info;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.shiro.realm.AuthenticatingRealm#doGetAuthenticationInfo(org
	 * .apache.shiro.authc.AuthenticationToken)
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken authcToken) throws AuthenticationException {
		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
		UserBase user = userService
				.findByLoginName(token.getUsername());
		if (user != null) {
			if ("disabled".equals(user.getStatus())) {
				throw new DisabledAccountException();
			}

			byte[] salt = Encodes.decodeHex(user.getSalt());
			return new SimpleAuthenticationInfo(this.createMemberModel(user),
					user.getPassword(), ByteSource.Util.bytes(salt),
					getName());
		} else {
			return null;
		}
	}

	private UerModel createMemberModel(UserBase user) {
		UerModel userModel = new UerModel();
		userModel.id=user.getId();
		userModel.username=user.getUsername();
		userModel.email=user.getUserEmail();
		userModel.mobile=user.getUserMobile();
		return userModel;

	}
	/**
	 * 设定Password校验的Hash算法与迭代次数.
	 */
	@PostConstruct
	public void initCredentialsMatcher() {
		HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(HASH_ALGORITHM);
		matcher.setHashIterations(HASH_INTERATIONS);

		setCredentialsMatcher(matcher);
	}
	/**
	 * 自定义Authentication对象，使得Subject除了携带用户的登录名外还可以携带更多信息.
	 */
	public static class UerModel implements Serializable {
		private static final long serialVersionUID = -1373760761780840081L;
		Long id;
		String username;
		String email;
		String mobile;
		String address;
		String sex;

		public Long getId() {
			return id;
		}
 

		public String getUsername() {
			return username;
		}

		 
		public String getEmail() {
			return email;
		}

		 
		public String getMobile() {
			return mobile;
		}

		 
		public String getAddress() {
			return address;
		}
 

		public String getSex() {
			return sex;
		}

 


		/**
		 * 本函数输出将作为默认的<shiro:principal/>输出.
		 */
		@Override
		public String toString() {
			return username;
		}

		/**
		 * 重载equals,只计算loginName;
		 */
		@Override
		public int hashCode() {
			return HashCodeBuilder.reflectionHashCode(this, "username");
		}

		/**
		 * 重载equals,只比较loginName
		 */
		@Override
		public boolean equals(Object obj) {
			return EqualsBuilder.reflectionEquals(this, obj, "username");
		}
	}
}
